home *** CD-ROM | disk | FTP | other *** search
- Public Class CacheForm
- Inherits System.Web.UI.Page
- Protected WithEvents lblArea As System.Web.UI.WebControls.Label
- Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
-
- #Region " Web Form Designer Generated Code "
-
- 'This call is required by the Web Form Designer.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
-
- End Sub
-
- Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
- 'CODEGEN: This method call is required by the Web Form Designer
- 'Do not modify it using the code editor.
- InitializeComponent()
- End Sub
-
- #End Region
-
- Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- ' Check whether xml data is cached already.
- If Cache("Employees") Is Nothing Then
- CacheEmployeesData()
- End If
- ' Get the cached data.
- Dim xmldoc As System.Xml.XmlDocument = _
- DirectCast(Cache("Employees"), System.Xml.XmlDocument)
-
- ' display all the Cache items
- DisplayCacheContents()
- End Sub
-
- ' Display the contents of the Cache object.
-
- Sub DisplayCacheContents()
- Dim de As DictionaryEntry
- Dim msg As String
- For Each de In Cache
- msg &= String.Format("<b>{0}</b> = {1}<br>", de.Key, de.Value)
- Next
- ' Display the result in a Label control.
- lblMessage.Text = msg
- Diagnostics.Debug.WriteLine(msg)
- End Sub
-
- End Class
-
- ' The following functions are in a module, because they must
- ' be accessible from the code in Global.asax file.
-
- ' You can test several cache options by assigning a different
- ' value to the following constant
- ' 0 = just store in the Cache object
- ' 1 = make the "employees" Cache item dependent on a file, and
- ' the "EmployeeCacheTime" item depending on "employee"
- ' 2 = make the "employees" item expire after an absolute timeout
- ' 3 = make the "employees" item expire after a timeout of inactivity
- ' 4 = same as (1), but specify an OnRemove callback routine
- ' 5 = same as (1), but specify that the tracking shouldn't start immediately
- ' (this option doesn't work as documented, read remarks below)
-
- #Const CACHE_ACTION = 0
-
- Module CacheFunctions
- Public FILENAME As String = HttpContext.Current.Server.MapPath("employees.xml")
-
- ' Save a reference to the Cache object.
- Dim Cache As System.Web.Caching.Cache = HttpRuntime.Cache
-
- ' this routine reads the employees.xml file into an XmlDocument object
-
- Sub CacheEmployeesData()
- ' Display a diagnostic message.
- Diagnostics.Debug.WriteLine("Refreshing Cache Element")
-
- ' Read an XML document.
- Dim xmldoc As New System.Xml.XmlDocument()
- xmldoc.Load(FILENAME)
-
- #If CACHE_ACTION = 0 Then
- ' SIMPLE CACHING
- ' Store it in the Cache object.
- Cache("Employees") = xmldoc
- ' Remember caching time as well.
- Cache("EmployeesCacheTime") = Date.Now
-
- #ElseIf CACHE_ACTION = 1 Then
- ' Add the XML data to the cache, making it dependent on the specified file name.
- Dim xmldocDep As New System.Web.Caching.CacheDependency(filename)
- Cache.Insert("Employees", xmldoc, xmldocDep)
-
- ' Add the insertion time as well, making it dependent on the preceding item.
- Dim cacheKeys() As String = {"Employees"}
- Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
- Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
-
- #ElseIf CACHE_ACTION = 2 Then
- ' Make this item expire after 10 seconds (absolute expiration).
- Cache.Insert("Employees", xmldoc, Nothing, Now.AddSeconds(10), Cache.NoSlidingExpiration)
- ' Add insertion time as well.
- Dim cacheKeys() As String = {"Employees"}
- Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
- Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
-
- #ElseIf CACHE_ACTION = 3 Then
- ' Make this item expire after 10 seconds of inactivity.
- Cache.Add("Employees", xmldoc, Nothing, Cache.NoAbsoluteExpiration, New TimeSpan(0, 0, 10), Caching.CacheItemPriority.Low, nothing)
-
- ' Add insertion time as well.
- Dim cacheKeys() As String = {"Employees"}
- Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
- Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
-
- #ElseIf CACHE_ACTION = 4 Then
- ' Add the XML data to the cache, making it dependent on the specified file name.
- Dim xmldocDep As New System.Web.Caching.CacheDependency(filename)
- Cache.Insert("Employees", xmldoc, xmldocDep, Nothing, Nothing, Caching.CacheItemPriority.Default, AddressOf OnRemoveItem)
-
- ' Add insertion time as well.
- Dim cacheKeys() As String = {"Employees"}
- Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
- Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
-
- #ElseIf CACHE_ACTION = 5 Then
- ' Add the XML data to the cache, making it dependent on the specified file name
- ' but start tracking after a while
- ' NOTE THAT THE FOLLOWING DOESN'T WORK AS DOCUMENTED
- ' It should start the tracking after the specified timeout, but it appears to
- ' start it immediately
- Dim xmldocDep As New System.Web.Caching.CacheDependency(filename, Now.AddSeconds(15))
- Cache.Insert("Employees", xmldoc, xmldocDep, Nothing, Nothing, Caching.CacheItemPriority.Default, AddressOf OnRemoveItem)
-
- ' Add insertion time as well.
- Dim cacheKeys() As String = {"Employees"}
- Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
- Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
- #End If
-
- End Sub
-
- #If 0 Then
- Sub CacheEmployeesData()
- ' Read an XML document.
- Dim xmldoc As New System.Xml.XmlDocument()
- xmldoc.Load(FILENAME)
-
- ' Make this item expire when the file is modified, and
- ' call a callback procedure when this happens.
- Dim xmldocDep As New System.Web.Caching.CacheDependency(FILENAME, Now.AddSeconds(10))
- Cache.Insert("Employees", xmldoc, xmldocDep, Nothing, Nothing, _
- Caching.CacheItemPriority.Default, AddressOf OnRemoveItem)
-
- ' Add the insertion time as well, making it dependent on the preceding item.
- Dim cacheKeys() As String = {"Employees"}
- Dim timeDep As New System.Web.Caching.CacheDependency(Nothing, cacheKeys)
- Cache.Insert("EmployeesCacheTime", Date.Now, timeDep)
- End Sub
- #End If
-
- ' this routine is called when the item is removed from the Cache object
-
- Sub OnRemoveItem(ByVal key As String, ByVal value As Object, ByVal reason As Caching.CacheItemRemovedReason)
- ' Refresh the XML file when the cached item expires.
- CacheEmployeesData()
- End Sub
-
- ' a lock object
- Dim counterLock As New Object()
-
- ' this routine shows how you can increment a counter in the cache
-
- Sub IncrementCounter()
- Diagnostics.Debug.WriteLine("Before SyncLock " & Now)
- Diagnostics.Debug.WriteLine("After SyncLock " & Now)
- SyncLock counterLock
- If Cache("counter") Is Nothing Then
- Cache("counter") = 0
- Else
- ' cause a long pause - its purpose is to show that other
- ' pages (in other threads) have to wait, as you can see
- ' from the messages in the Diagnostic window
- Threading.Thread.Sleep(5000)
- If CInt(Cache("counter")) > 1 Then Cache("counter") = CInt(Cache("counter")) - 1
- End If
- End SyncLock
-
- ' in this case we're using the same Cache element to lock itself
- If Cache("msg") Is Nothing Then
- Cache("msg") = "*"
- Else
- Diagnostics.Debug.WriteLine("Before SyncLock " & Now)
- SyncLock Cache("msg")
- Diagnostics.Debug.WriteLine("After SyncLock " & Now)
- Threading.Thread.Sleep(5000)
- Cache("msg") = Cache("msg").ToString & "*"
- End SyncLock
- End If
- End Sub
-
- End Module